package com.pur.zsms.matermanage.mater.web;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.filter.AttributeListenerUtil;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.ApplicationUtil;
import com.pur.zsms.matermanage.mater.po.MaterTypePo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterTypeVo;
import com.pur.zsms.matermanage.mater.vo.MaterValueVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.MaterVoForSup;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterSupplierVo;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;

@Controller
@RequestMapping(value = "/materType")
public class MaterTypeControllor {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(MaterTypeControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "materService")
	private IMaterService materService;

	@Resource(name = "shopService")
	private IShopService shopService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "dictionaryService")
	private IDataDictionaryService dataDictionaryService;

	@RequestMapping(value = "/findMaterTypeForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findMaterTypeForSup(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			MaterTypeVo materTypeVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		materTypeVo.setSupId(supplierService.getSupIdByUserId(userInfo.getUserId()));
		this.pageList = materService.findMaterTypeForSup(pageController, materTypeVo);
		List<MaterTypeVo> materTypeList = new ArrayList<MaterTypeVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				MaterTypeVo f = new MaterTypeVo();
				f.setMaterType((Integer) item[0]);
				f.setMaterTypeName((String) item[1]);
				f.setOrderId((Integer) item[2]);
				f.setParent((Integer) item[3]);
				f.setLevel((Integer) item[4]);
				f.setSupId(Long.valueOf(item[5].toString()));
				f.setIsActive((String) item[6]);
				materTypeList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", materTypeList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/preSaveMaterTypeForSup.do", method = { RequestMethod.GET })
	public ModelAndView preSaveMaterTypeForSup(MaterTypeVo materTypeVo, HttpSession session) {
		// System.out.println(AttributeListenerUtil.getSessions().size());
		JSONObject json = new JSONObject();
		Map map = new HashMap();
		/*
		 * UserPo userInfo = (UserPo) session.getAttribute("user"); try {
		 * materTypeVo
		 * .setSupId(supplierService.getSupIdByUserId(userInfo.getUserId()));
		 * 
		 * MaterTypePo materTypePo = materService.getMaterTypePo(materTypeVo);
		 * // 查询所有角色信息 map.put("materTypePo", materTypePo);
		 * 
		 * } catch (GenericException e) { e.printStackTrace(); }
		 */
		return new ModelAndView("/matermanage/mater/addMaterTypeForSup", map);
	}

	@RequestMapping(value = "/preUpdateMaterTypeForSup.do", method = { RequestMethod.GET })
	public ModelAndView preUpdateMaterTypeForSup(MaterTypeVo materTypeVo, HttpSession session) {
		// System.out.println(AttributeListenerUtil.getSessions().size());
		JSONObject json = new JSONObject();
		Map map = new HashMap();
		UserPo userInfo = (UserPo) session.getAttribute("user");
		try {
			materTypeVo.setSupId(supplierService.getSupIdByUserId(userInfo.getUserId()));

			MaterTypePo materTypePo = materService.getMaterTypePo(materTypeVo); // 查询所有角色信息
			map.put("materTypePo", materTypePo);

		} catch (GenericException e) {
			e.printStackTrace();
		}
		return new ModelAndView("/matermanage/mater/updateMaterTypeForSup", map);
	}

	@RequestMapping(value = "/pulldownMaterTypeParentForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownMaterTypeParentForSup(String space, HttpSession session) {
		String provicePullJson = "";
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			provicePullJson = materService.pulldownMaterTypeParentForSup(space, supplierService.getSupIdByUserId(userInfo.getUserId()));
		} catch (GenericException e) {
			e.printStackTrace();
		}
		return provicePullJson;
	}

	@RequestMapping(value = "/saveMaterTypeForSupId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveMaterTypeForSupId(MaterTypeVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			c.setSupId(supplierService.getSupIdByUserId(userInfo.getUserId()));
			boolean flag = materService.addMaterTypePo(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", materService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/updateMaterTypeForSupId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateMaterTypeForSupId(MaterTypeVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			c.setSupId(supplierService.getSupIdByUserId(userInfo.getUserId()));
			boolean flag = materService.updateMaterTypePo(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", materService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

}
